<template>
  <a-card>
    <a-form-model ref="ruleForm" :model="fileSerDTOData" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" :rules="editModel != 0 ? rules : {}">
      <title-name title="基础信息"></title-name>
      <custom-form :isEmit="false" :formList="formList" :form="fileSerDTOData" :editModel="editModel == 0 ? '' : ''">
        <template v-slot:table>
          <a-form-model-item class="mb20" label=" " :colon="false">
            <s-table bordered ref="table" tableLayout="fixed" size="default" rowKey="key" :columns="columns" :data="loadData" :alert="true" show-size-changer show-quick-jumper :showPagination="false">
              <template slot="deductionId" slot-scope="text, record">
                <div class="form-item">
                  <template v-if="record.deductionTypeId == '分公司'">
                    <SelectSubCompany disabled :getAll="true" :autoSelected="false" :hiddenSelfBran="true" v-model="record.deductionId" placeholder="请选择" />
                  </template>
                  <template v-else-if="record.deductionTypeId == '分包商'">
                    <SelectSubcontractor disabled placeholder="请选择" v-model="record.deductionId" :form="fileSerDTOData" :isClear="editModel == 2" keyValue="projectId"></SelectSubcontractor>
                  </template>
                  <template v-else>
                    <BaseInput disabled v-model="record.deductionId" placeholder="请输入" />
                  </template>
                </div>
              </template>
              <template slot="deductionAmount" slot-scope="text, record">
                <div class="form-item">
                  <BaseInputNumber :disabled="editModel == 0 || currentStep != 3" placeholder="请输入" v-model="record.deductionAmount"></BaseInputNumber>
                </div>
              </template>
              <template slot="deductionList" slot-scope="text, record">
                <l-table v-if="editModel != 0" business-code="BM07" :upload-arrys.sync="record.deductionList" />
                <file-link v-else :file-list="record.deductionList" />
              </template>
              <template slot="action" slot-scope="text, record, index">
                <a-popconfirm title="确定要删除?" @confirm="() => (fileSerDTOData.serDTOS.splice(index, 1), $refs.table.refresh(true))">
                  <a>删除</a>
                </a-popconfirm>
              </template>
            </s-table>
          </a-form-model-item>
        </template>
      </custom-form>
    </a-form-model>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top" v-if="editModel != 0 && currentStep == 3">
      <a-button class="button_right" type="primary" :loading="loadingBtn.SUBMITTED" @click="handleSubmit('SUBMITTED')">提交</a-button>
      <a-button @click="handleActiveClose">关闭</a-button>
    </div>
  </a-card>
</template>

<script>
import { updateDataSummary } from '@/api/businessAffairs/subcontractVisa'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import BSelect from '@/components/BComponents/BSelect'
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },

  {
    title: '扣款单位',
    dataIndex: 'deductionId',
    width: 300,
    scopedSlots: { customRender: 'deductionId' }
  },
  {
    title: '扣除金额',
    dataIndex: 'deductionAmount',
    width: 200,
    scopedSlots: { customRender: 'deductionAmount' }
  },
  {
    title: '联系函',
    dataIndex: 'deductionList',
    scopedSlots: { customRender: 'deductionList' }
  }
]

export default {
  name: 'dataSummary',
  components: {
    SelectSubCompany,
    SelectProject,
    SelectSubcontractor,
    BSelect
  },
  props: {
    fileSerDTOData: {
      type: Object,
      default() {
        return {}
      }
    },
    currentStep: {
      type: [Number, String],
      default: -1
    },
    editModel: {
      type: [Number, String],
      default: 2
    }
  },
  data() {
    this.columns = columns

    return {
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      findCountQueryParam: {
        pageNo: 1,
        pageSize: 10000,
        query: {}
      },
      loadData: () => {
        return new Promise((resolve, reject) => {
          resolve({
            data: this.fileSerDTOData.fineDTOS || []
          })
        })
      },
      //单位类型
      unitTypeList: [
        {
          label: '分公司',
          value: '分公司'
        },
        {
          label: '分包商',
          value: '分包商'
        },
        {
          label: '非过账甲指分包',
          value: '非过账甲指分包'
        }
      ],
      rules: {
        visaOddJob: [{ required: true, message: '请输入' }]
      }
    }
  },

  methods: {
    /**
     * 表单验证
     */
    validateForm() {
      let flag1 = true
      let flag2 = true
      this.$refs.ruleForm.validate((e) => {
        flag1 = e
      })
      if (this.fileSerDTOData.isOtherDeduction == 1) {
        this.fileSerDTOData.fineDTOS.forEach((i) => {
          if (!i.deductionAmount && flag2) {
            flag2 = false
          }
        })
        if (!flag2) {
          this.$message.warning('请填写表格数据')
        }
      }

      if (flag1 && flag2) {
        return true
      } else {
        this.$message.warning("请填写完所有必填项信息")
        return false
      }
    },
    /**
     * 提交表单
     */
    handleSubmit(status) {
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }

      this.okSubmit(status)
    },
    okSubmit(status) {
      this.loading = true
      this.loadingBtn[status] = true

      let fileSerDTOData = this.$clone(this.fileSerDTOData)
      fileSerDTOData.auditStatus = 'COMMIT'
      // let api = [updateVisaSingle, addVisaSingle][this.editModel - 1] // 根据操作切换url地址
      updateDataSummary(fileSerDTOData)
        .then(() => {
          this.loading = false
          this.loadingBtn[status] = false
          this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
          this.$multiTab.close()
        })
        .catch(() => {
          this.loadingBtn[status] = false
          this.loading = false
          this.$message.warning(err.data.message)
        })
    },
    /**
     * 动态关闭
     */
    handleActiveClose() {
      this.isBack ? this.$router.go(-1) : this.businessId ? window.close() : this.$multiTab.close()
    }
  },
  computed: {
    formList() {
      return [
        {
          keyName: 'initialVisaEnd',
          type: 'BaseInputNumber',
          disabled: true,
          label: '审定签证金额(不含税)(元)',
          precision: 2
        },
        {
          keyName: 'initialVisaTaxEnd',
          type: 'BaseInputNumber',
          disabled: true,
          label: '审定签证税金(元)',
          precision: 2
        },
        {
          keyName: 'initialVisaTotalEnd',
          type: 'BaseInputNumber',
          disabled: true,
          label: '审定签证金额(含税)(元)'
        },
        {
          keyName: 'isOtherDeduction',
          type: 'a-radio-group',
          disabled: true,
          label: '是否涉及其他单位扣款事宜',
          data: [
            {
              label: '否',
              value: 0
            },
            {
              label: '是',
              value: 1
            }
          ]
        },
        {
          slot: true,
          hidden: this.fileSerDTOData.isOtherDeduction != 1,
          customItemSlot: true,
          keyName: 'table',
          span: 24
        },
        {
          keyName: 'visaOddJob',
          type: 'BaseInputNumber',
          disabled: this.editModel == 0,
          label: '签证零工(含税)(元)'
        },
        {
          keyName: 'fileInfoVOS',
          type: 'uploadFile',
          label: '分包签盖资料上传',
          disabled: this.editModel == 0,
          span: 24,
          businessCode: 'BM07'
        }
      ]
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .form-item  {
  .ant-input:not([disabled]),
  .ant-select:not(.ant-select-disabled) .ant-select-selection,
  .ant-calendar-picker-input,
  .ant-input-number:not([disabled]),
  .ant-cascader-picker {
    border-color: #e4ac0b;
    background-color: #fff9e8;
  }
  .ant-input-disabled,
  .ant-input-number-disabled .ant-input-number-input,
  .ant-time-picker-input[disabled] {
    border-color: #d9d9d9 !important;
    background-color: #f5f5f5 !important;
  }
  .remark {
    .ant-input {
      border-color: inherit;
      background-color: initial;
    }
  }
}
</style>
